﻿<!DOCTYPE html />
<html lang="en">
<head>
    <title id='Description'>jqxChart events example</title>
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdraw.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxchart.core.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxchart.rangeselector.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            // prepare the data
            var source =
            {
                datatype: "tsv",
                datafields: [
                    { name: 'Date' },
                    { name: 'SPOpen' },
                    { name: 'SPHigh' },
                    { name: 'SPLow' },
                    { name: 'SPClose' },
                    { name: 'SPVolume' },
                    { name: 'SPAdjClose' }
                    ],
                url: '../sampledata/nasdaq_vs_sp500_detailed.txt'
            };

            var dataAdapter = new $.jqx.dataAdapter(source, { async: false, autoBind: true, loadError: function (xhr, status, error) { alert('Error loading "' + source.url + '" : ' + error); } });
            var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

            var toolTipCustomFormatFn = function (value, itemIndex, serie, group, categoryValue, categoryAxis) {
                var dataItem = dataAdapter.records[itemIndex];
                var volume = dataItem.SPVolume;

                return '<DIV style="text-align:left"><b>Date: ' +
                        categoryValue.getDate() + '-' + months[categoryValue.getMonth()] + '-' + categoryValue.getFullYear() +
                        '</b><br />Index value: ' + value +
                        '</b><br />Daily volume: ' + volume
                '</DIV>';
            };

            // prepare jqxChart settings
            var settings = {
                title: "S&P 500 Index value and daily volume",
                description: "(June 2010 - November 2014)",
                enableAnimations: true,
                animationDuration: 1500,
                enableCrosshairs: true,
                padding: { left: 20, top: 5, right: 20, bottom: 5 },
                colorScheme: 'scheme17',
                source: dataAdapter,
                xAxis:
                    {
                        dataField: 'Date',
                        type: 'date',
                        valuesOnTicks: true,
                        minValue: new Date(2014, 1, 1),
                        maxValue: new Date(2014, 10, 1),
                        labels:
                        {
                            formatFunction: function (value) {
                                return value.getDate() + '-' + months[value.getMonth()] + '<br>' + value.getFullYear().toString();
                            }
                        },
                        gridLines: {visible: false},
                        rangeSelector: {
                            // Uncomment the line below to render the selector in a separate container 
                            //renderTo: $('#selectorContainer'),
                            size: 100,
                            padding: { top: 10, bottom: 0 },
                            backgroundColor: 'white',
                            dataField: 'SPClose',
                            baseUnit: 'month',
                            showGridLines: false,
                            formatFunction: function (value) {
                                return months[value.getMonth()] + '\'' + value.getFullYear().toString().substring(2);
                            }
                        }
                    },
                seriesGroups:
                    [
                        {
                            type: 'line',
                            linesUnselectMode: 'click',
                            toolTipFormatFunction: toolTipCustomFormatFn,
                            valueAxis:
                            {
                                title: {text: 'S&P 500<br>'},
                                gridLines: {visible: false},
                            },
                            series: [
                                {
                                    dataField: 'SPClose',
                                    displayText: 'S&P Index Value',
                                    lineWidth: 1
                                }
                            ]
                        },
                        {
                            type: 'area',
                            toolTipFormatFunction: toolTipCustomFormatFn,
                            linesUnselectMode: 'click',
                            valueAxis:
                            {
                                position: 'right',
                                title: {text: '<br>Daily Volume'},
                                gridLines: {visible: false},
                                labels:
                                {
                                    formatFunction: function (value) {
                                        return value / 1000000 + 'M';
                                    }
                                }
                            },
                            series: [
                                {
                                    dataField: 'SPVolume',
                                    displayText: 'S&P Index Volume',
                                    lineWidth: 1,
                                    opacity: 0.2
                                }
                            ]
                        }
                    ]
            };

            function dumpEventInfo(event) {
                var args = event.args;

                if (event.type.indexOf('refresh') != -1) {
                    if ($('#chartContainer').jqxChart('getInstance') != args.instance)
                        return;
                }

                var text = $('#eventsTextArea').val();
                var date = new Date();
                var line = 'Time: ' + (date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()) + ', event: ' + event.type;
                if (args.elementIndex)
                    line += ', element index: ' + args.elementIndex;

                if (args.elementValue) {
                    line += ", value: " + args.elementValue;
                }

                if (event.type == 'toggle')
                {
                    line += ", series group index: " + settings.seriesGroups.indexOf(args.seriesGroup);
                    line += ", visible: " + args.state;
                }

                if (event.type.indexOf('rangeSelection') != -1) {
                    line += ", minValue: " + args.minValue.getFullYear() + '-' + (args.minValue.getMonth() + 1) + '-' + args.minValue.getDate();
                    line += ", maxValue: " + args.maxValue.getFullYear() + '-' + (args.maxValue.getMonth() + 1) + '-' + args.maxValue.getDate();
                }

                text = line + '\n' + text;
                $('#eventsTextArea').val(text);
            }

            $('#chartContainer').jqxChart(settings)
                .on('click mouseover mouseout toggle rangeSelectionChanging rangeSelectionChanged refreshBegin refreshEnd', function (event) {
                    if (event.args)
                        dumpEventInfo(event);
                });
        });
    </script>
</head>
<body class='default'>
    <div>
        <div id='chartContainer' style="width:800px; height:500px;">
        </div>
        <br />
        <textarea id="eventsTextArea" style="width:800px; height: 200px"></textarea>
    </div>
</body>
</html>